//
//  CityManager.m
//  LzzizDemo
//
//  Created by xbnzd on 16/11/8.
//  Copyright © 2016年 xbnzd. All rights reserved.
//

#import "CityManager.h"
#import "ProvinceModel.h"
#import "CitysModel.h"



@implementation CityManager

+ (instancetype)shareManager
{
    static CityManager *c = nil;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        c = [[CityManager alloc] init];
    });
    
    return c;

}


/**
 创建plist==弃用

 @return <#return value description#>
 */
/*
+ (NSString *)createPlist
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *filename=[path stringByAppendingPathComponent:@"city.plist"];   //获取路径
    
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:filename]) {
         [fm createFileAtPath:filename contents:nil attributes:nil];
    }
   
    
    return filename;
}
*/

/**
 返回plist中的原始数组数据

 */
+ (NSMutableArray *)reurnCityListArr
{
//    NSString *str = [CityManager createPlist];
    NSString *str = [[NSBundle mainBundle]pathForResource:@"city" ofType:@"plist"];
    NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:str];
    
    return arr;
}


/**
 写入请求到的城市列表数据==第一版弃用

 @param provinceArr <#provinceArr description#>
 */
/*
+ (void)writeCityListWithArr:(NSArray *)provinceArr
{
    NSString *p = [CityManager createPlist];
    
    [provinceArr writeToFile:p atomically:YES];
}
*/


/**
 检查本地是否已经存储过城市数据

 @return <#return value description#>
 */
/*
+ (BOOL)checkHaveCityList
{
    NSMutableArray *arrTemp = [self reurnCityListArr];
    
    if (arrTemp.count > 0) {
        return YES;
    }
    
    return NO;
}
*/

/**
 返回模型数组,直接用在城市列表

 */
+ (NSArray *)returnDataArrForTableView
{
    NSArray *arr = [CityManager shareManager].dataArrForTableView;
    if (arr.count == 0) {
       NSMutableArray *mutableArr = [[NSMutableArray alloc] init];
        
        NSMutableArray *arrTemp = [self reurnCityListArr];
        for (int i = 0; i < arrTemp.count; i ++) {
            NSDictionary *dic = arrTemp[i];
            ProvinceModel *m = [[ProvinceModel alloc] init];
            m.enName = dic[@"enName"];
            m.arName = dic[@"arName"];
            m.provinceNO = dic[@"province"];
            NSMutableArray *provinceCityArr = [[NSMutableArray alloc] init];
            NSArray *cityArrTemp = dic[@"cityCode"];
            for (int j = 0; j < cityArrTemp.count; j ++) {
                NSDictionary *cityDic = cityArrTemp[j];
                CitysModel *cityM = [[CitysModel alloc] init];
                cityM.enName = cityDic[@"enName"];
                cityM.arName = cityDic[@"arName"];
                cityM.cityNO = cityDic[@"city"];
                [provinceCityArr addObject:cityM];
            }
            m.citysArr = [provinceCityArr copy];
            
            [mutableArr addObject:m];
        }
        
        [CityManager shareManager].dataArrForTableView = [mutableArr copy];
    }
    
    return [CityManager shareManager].dataArrForTableView;
}



/**
 返回用于搜索联想的数据数组

 */
+ (NSArray *)returnDataArrForSearchRemind
{
    NSArray *arr = [CityManager shareManager].dataArrForSearchCityRemind;
    if (arr.count == 0) {
       NSMutableArray *mutableArr = [[NSMutableArray alloc] init];
        
        NSMutableArray *arrTemp = [self reurnCityListArr];
        for (int i = 0; i < arrTemp.count; i ++) {
            NSDictionary *dic = arrTemp[i];
            NSMutableArray *provinceCityArr = [[NSMutableArray alloc] init];
            NSArray *cityArrTemp = dic[@"cityCode"];
            for (int j = 0; j < cityArrTemp.count; j ++) {
                NSDictionary *cityDic = cityArrTemp[j];
                CitysModel *cityM = [[CitysModel alloc] init];
                cityM.enName = cityDic[@"enName"];
                cityM.arName = cityDic[@"arName"];
                cityM.cityNO = cityDic[@"city"];
                [provinceCityArr addObject:cityM];
                
                [mutableArr addObject:cityM];
            }
            
            
        }
        
        [CityManager shareManager].dataArrForSearchCityRemind = [mutableArr copy];
    }
    
    return [CityManager shareManager].dataArrForSearchCityRemind;

}


/**
 关键字筛选出相应的列表

 @param keyStr <#keyStr description#>
 @return <#return value description#>
 */
+ (NSArray *)searchRemindwithKey:(NSString *)keyStr
{
    NSPredicate *p;
    
    if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
        p = [NSPredicate predicateWithFormat:@"arName CONTAINS[cd] %@",keyStr];
    }
    else {
        p = [NSPredicate predicateWithFormat:@"enName CONTAINS[cd] %@",keyStr];
    }
    
    NSArray *arr = [[CityManager shareManager].dataArrForSearchCityRemind filteredArrayUsingPredicate:p];
    
    return arr;
}

/**
 根据关键字查询出相应的model
 
 @param keyStr 关键字
 @return <#return value description#>
 */
+ (CitysModel *)searchTheModelWithKeyWord:(NSString *)keyStr
{
    NSPredicate *p;
    
    if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
        p = [NSPredicate predicateWithFormat:@"arName CONTAINS[cd] %@",keyStr];
    }
    else {
        p = [NSPredicate predicateWithFormat:@"enName CONTAINS[cd] %@",keyStr];
    }

    NSArray *arr = [[CityManager shareManager].dataArrForSearchCityRemind filteredArrayUsingPredicate:p];
    if (arr.count > 0) {
         return arr[0];
    }else{//如果定位城市不是列表中的.随意返回城市编号
        CitysModel *m = [[CitysModel alloc] init];
        m.enName = keyStr;
        m.arName = keyStr;
        m.cityNO = @"00001";
        
        return m;
    }
    
    return nil;
}


/**
 返回定位失败,或者没开启定位是的城市

 @return <#return value description#>
 */
+ (CitysModel *)returnDefaultCityWhenLocationFailed
{
    CitysModel *model = [CitysModel getCityModel];
    if (model == nil) {
        // 如果没有保存过城市，则取默认城市作为当前城市
        model = [CityManager shareManager].dataArrForSearchCityRemind[0];
        [CitysModel setCityMessageWithCityEntity:model];
    }
    return model;
}

@end
